<?php

class Kit_Controller extends WakEngineController {

    function _setUp() {
        $id=$this->response->shiftNode();
        $this->response->id=$id;
        // getting kit
        if (!$this->kit=Webappkit::getKit($id)) {
            $this->_setAction('badkit');
            return true;
        }
        $this->response->kit=$this->kit;
        $this->response->cfg=$this->kit->getCfg();
    }

    /**
     *
     */
    function index() {

        $kit=&$this->kit;
        $this->response->subkits=$kit->listSubkits(); //print_r($this->subkits);
            // does the kit provide unit tests ?
            if ($kit and $tests=$kit->getTools('SimpleTestTool')) {
                //$this->response->tests=$tests;
                // print_r($tests);
                $this->response->tests=$tests['tests']->run();
                /*foreach ($tests as $tool) {
                    $this->response->tests_reports[]=$tool->run();
                }*/
            }
        // is there some doc ?
        /*if (is_file($kit->getpath().'docs/index.rest.txt')) {
            $txt=join('',file($kit->getpath().'docs/index.rest.txt')); // echo '<pre>'.$txt;
            webappkit::getkit('text_rest');
            $tree=text_rest::parse($txt);
            $formatter=Text_reST_Formatter::factory('html');
            $this->response->doc=$formatter->format($tree);
            // echo '<pre>'; print_r($doc);
        } else $this->response->doc=null;*/
    }

    function tests() {
        return $this->_runKitTests($this->response->kit);
    }

    function badkit() {

    }

    /**
     * executes a kit's embedded unit tests
     * @param Webappkit $kit
     */
    function _runKitTests(& $kit) {
        $report=$kit->tests->run();
        $this->response->tests=$report;
        $this->response->view='kit.tests';
        return true;
    }
}
?>
